﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;


namespace ZhimeiSoft.CharmManager.Client.Forms.Control.Reservation
{
    public partial class frmTodayReservationCustom : FormBase
    {
        /// <summary>
        /// 当前预约的会员
        /// </summary>
        private DataRow customRow = null;
        /// <summary>
        /// 预约记录
        /// </summary>
        private DataRow reservationRow = null;
        /// <summary>
        /// 预约项目关系表
        /// </summary>
        private DataTable dtReservationDetail = null;
        /// <summary>
        /// 预约主记录
        /// </summary>
        private DBEntityReservation reservationInfo = null;
        /// <summary>
        /// 员工信息
        /// </summary>
        private DataSet dsEmployee = new DataSet();
        /// <summary>
        /// 员工ID 和显示MAP
        /// </summary>
        private SortedList<int, string> srotEmployeeName = new SortedList<int, string>();

        public frmTodayReservationCustom() : base()
        {
            InitializeComponent();
            initData();
            eventSerchCustom(customRow);
        }

        public frmTodayReservationCustom(DataRow drCustomInfo, DataRow drReservation):this()
        {
            customRow = drCustomInfo;
            reservationRow = drReservation;
            //预约说明
            txtRMemo.Text = drReservation[DBEntityReservation.FieldName.RMEMO].ToString();
            cbxState.SelectedIndex = 0;
            reservationInfo = new DBEntityReservation(drReservation);
            initFrom(reservationInfo);
            eventSerchCustom(drCustomInfo);
        }

        private void initFrom(DBEntityReservation reservationInfo)
        {
            string error = string.Empty;
            DataSet dsResverationServiceItem = new DataSet();
            if (!BussinessProxyManager.Instance.QueryReservationServiceItem(reservationInfo.RESERVATIONID, out dsResverationServiceItem, out error))
            {
                MsgBox.MsgInfo("初始化预约项目错误：" + error);
                return;
            }
            dtReservationDetail = dsResverationServiceItem.Tables[0].Copy();

        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        private void initData()
        {
            dateInputReservation.Value = DateTime.Now;
            customSearch.onSerchCustom += eventSerchCustom;
            hourTimeSimpleControl.CurrDateTime = DateTime.Now;
            DataSet dsResverationServiceItem = new DataSet();
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.QueryReservationServiceItem(-1, out dsResverationServiceItem, out error))
            {
                MsgBox.MsgInfo("初始化预约项目错误：" + error);
                return;
            }
            if (!BussinessProxyManager.Instance.QueryEmployeeState(string.Empty, out dsEmployee, out error))
            {
                MsgBox.MsgInfo("初始化员工信息错误：" + error);
                return;
            }
            foreach (DataRow drEmployee in dsEmployee.Tables[0].Rows)
            {
                //初始化员工信息
                repositoryItemComboBoxEmployeeName.Items.Add(drEmployee[DBEntityEmployee.FieldName.ENAME].ToString());
                srotEmployeeName.Add(Convert.ToInt32(drEmployee[DBEntityEmployee.FieldName.EMPLOYEEID]), drEmployee[DBEntityEmployee.FieldName.ENAME].ToString());
            }

            dtReservationDetail = dsResverationServiceItem.Tables[0].Copy();
            gdServiceItem.DataSource = dtReservationDetail;
        }
        /// <summary>
        /// 查询后
        /// </summary>
        /// <param name="drCustomInfo"></param>
        private void eventSerchCustom(DataRow drCustomInfo){
            if (drCustomInfo == null)
                return;

            customRow = drCustomInfo;
            labCustomName.Text = "会员：" + customRow[DBEntityCustomInfo.FieldName.CNAME].ToString();
            labNickName.Text = "昵称：" + customRow[DBEntityCustomInfo.FieldName.CNICKNAME].ToString();
            labTelPhone.Text = "电话：" + customRow[DBEntityCustomInfo.FieldName.CMOBILEPHONE].ToString();
            labLastRegister.Text = "上次到店：" + Convert.ToDateTime(customRow[DBEntityCustomInfo.FieldName.CLASTREGISTERDATE]).ToShortDateString()
                + " " + Convert.ToDateTime(customRow[DBEntityCustomInfo.FieldName.CLASTREGISTERDATE]).ToShortTimeString();
        }
        /// <summary>
        /// 获取预约时间
        /// </summary>
        /// <returns>返回时间</returns>
        public DateTime GetReservation()
        {
            return DateTime.Now;
        }

        private void textBoxItem1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //TODO 调用服务项目
            if (e.KeyChar.CompareTo('\r') == 0)
            {
                MsgBox.MsgInfo("调用服务项目进行快速添加！！");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (customRow == null)
            {
                MsgBox.MsgInfo("请选择预约的会员。");
                SetErrorControl(labCustomName, "请选择预约的会员");
                return;
            }
            gvServiceItem.UpdateCurrentRow();
            if (gvServiceItem.RowCount == 0)
            {
                MsgBox.MsgInfo("请选择预约项目。");
                SetErrorControl(gdServiceItem, "请选择预约项目");
                return;
            }
            //TODO 检测一下预约的美容师是否休假

            DBEntityReservation reservationInfo = new DBEntityReservation();

            reservationInfo.CUSTOMID = Convert.ToInt32(customRow[DBEntityCustomInfo.FieldName.CUSTOMID]);
            //TODO 检测操作开始结束时间
            reservationInfo.RWORKSTARTTIME = DateTime.Now;
            reservationInfo.RWORKENDTIME = DateTime.Now;
            reservationInfo.RSTATE = DBEntityReservation.ENUM_RSTATE.ChineseStrToEnumStr(cbxState.SelectedItem.ToString());

            reservationInfo.RMEMO = txtRMemo.Text;
            reservationInfo.RESERVATIONTIME = dateInputReservation.Value;
            bool bresult = false;
            string error = string.Empty;
            DataSet dsReservationDetail = new DataSet();
            dsReservationDetail.Tables.Add(dtReservationDetail);
            if (reservationRow == null)
            {
                //插入
                BussinessProxyManager.Instance.AddResveration(reservationInfo, dsReservationDetail, out error);
            }
            else
            {
                
                //更新
                reservationInfo.RESERVATIONID = Convert.ToInt32(reservationRow[DBEntityReservation.FieldName.RESERVATIONID]);
                BussinessProxyManager.Instance.UpdateReservation(reservationInfo, dsReservationDetail, out error);
            }
            MsgBox.MsgInfo("保存成功。");
            this.DialogResult = DialogResult.OK;
        }
        private int GetResverationDetailTime()
        {
            int iReturn = 0;
            
            return iReturn;
        }
        private void gvServiceItem_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
        }

        private void gvServiceItem_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            gvServiceItem.UpdateCurrentRow();
            DataRow drDetail = gvServiceItem.GetDataRow(e.RowHandle);
            if (drDetail[DBEntityReservationServiceItem.FieldName.EMPLOYEEID] == null )
            {
                if (checkBoxEmployee.Checked)
                {//同一个美容师
                    DataRow[] drEmployee = dtReservationDetail.Select(DBEntityReservationServiceItem.FieldName.EMPLOYEEID + " IS NOT NULL");
                    if (drEmployee.Length != 0)
                    {
                        drDetail[DBEntityReservationServiceItem.FieldName.EMPLOYEEID] = drEmployee[0][DBEntityReservationServiceItem.FieldName.EMPLOYEEID];
                        return;
                    }
                }
                e.ErrorText = "请选择预约的美容师";
                e.Valid = false;
            }
        }

        private void addServiceItem_Click(object sender, EventArgs e)
        {
            
            ZhimeiSoft.CharmManager.Client.Forms.Control.ServiceItem.frmSelectServiceItem frmSelItem = new ZhimeiSoft.CharmManager.Client.Forms.Control.ServiceItem.frmSelectServiceItem();
            if (frmSelItem.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            List<DBEntityServiceItem> itemList = frmSelItem.ServiceItemList;
            foreach (DBEntityServiceItem serviceItem in itemList)
            {
                addReservationDetail(serviceItem);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void addReservationDetail(DBEntityServiceItem serviceItem)
        {
            DataRow drNew = dtReservationDetail.NewRow();

            drNew[DBEntityServiceItem.FieldName.SERVICEITEMID] = serviceItem.SERVICEITEMID;
            drNew[DBEntityServiceItem.FieldName.SNAME] = serviceItem.SNAME;
            drNew[DBEntityServiceItem.FieldName.SCATEGORYID] = serviceItem.SCATEGORYID;
            drNew[DBEntityServiceItem.FieldName.SPRICE] = serviceItem.SPRICE;
            drNew[DBEntityServiceItem.FieldName.STIMESPAN] = serviceItem.STIMESPAN;
            drNew[DBEntityServiceItem.FieldName.STATE] = serviceItem.STATE;
            drNew[DBEntityServiceItem.FieldName.SCALLBACKFLAG] = serviceItem.SCALLBACKFLAG;
            drNew[DBEntityServiceItem.FieldName.SCALLBACKDAY] = serviceItem.SCALLBACKDAY;
            drNew[DBEntityServiceItem.FieldName.STARTDATE] = serviceItem.STARTDATE;
            drNew[DBEntityServiceItem.FieldName.SENDDATE] = serviceItem.SENDDATE;
            drNew[DBEntityServiceItem.FieldName.SREMARK] = serviceItem.SREMARK;
            drNew[DBEntityServiceItem.FieldName.SAVAILABLEDAYS] = serviceItem.SAVAILABLEDAYS;
            drNew[DBEntityServiceItem.FieldName.SINDEXCODE] = serviceItem.SINDEXCODE;
            drNew[DBEntityServiceItem.FieldName.SERVICEITEMCODE] = serviceItem.SERVICEITEMCODE; 
            dtReservationDetail.Rows.Add(drNew);
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {

        }

        private void gvServiceItem_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle < 0)
            {
                return;
            }
            if (e.Column.FieldName.Equals(DBEntityEmployee.FieldName.EMPLOYEEID))
            {
                if (e.CellValue == null)
                    return;

                string displayText = srotEmployeeName[Convert.ToInt32(e.CellValue)].ToString();
                if (displayText.Equals(string.Empty))
                    return;
                e.DisplayText = displayText;
            }
        }
    }
}
